{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "start_time": "2023-04-29T10:39:04.325475Z",
     "end_time": "2023-04-29T10:39:04.447273Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3.14 2.17 0.   1.   2.  ]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "lst1 = [3.14, 2.17, 0, 1, 2]\n",
    "nd1 = np.array(lst1)\n",
    "print(nd1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0.0\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "print(torch.__version__)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T10:57:59.924961Z",
     "end_time": "2023-04-29T10:58:20.042737Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "是否支持 CUDA?: True\n",
      "tensor([10.], device='cuda:0')\n",
      "tensor([[-0.0019, -0.4102, -0.9059],\n",
      "        [-0.4365, -0.1098,  1.5498]], device='cuda:0')\n",
      "tensor([[ 9.9981,  9.5898,  9.0941],\n",
      "        [ 9.5635,  9.8902, 11.5498]], device='cuda:0')\n",
      "是否支持 cudnn: True\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    # 测试CUDA\n",
    "    print(\"是否支持 CUDA?:\", torch.cuda.is_available())\n",
    "    x = torch.tensor([10.0])\n",
    "    x = x.cuda()\n",
    "    print(x)\n",
    "\n",
    "    y = torch.randn(2, 3)\n",
    "    y = y.cuda()\n",
    "    print(y)\n",
    "\n",
    "    z = x + y\n",
    "    print(z)\n",
    "\n",
    "    # 测试CUDNN\n",
    "    from torch.backends import cudnn\n",
    "    print(\"是否支持 cudnn:\", cudnn.is_acceptable(x))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T11:04:34.300654Z",
     "end_time": "2023-04-29T11:04:36.641918Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([1., 2., 3., 4., 5.])"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.Tensor([1, 2, 3, 4, 5])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T11:09:52.690196Z",
     "end_time": "2023-04-29T11:09:52.741270Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[0.0000e+00, 6.0116e-43, 1.8754e+28],\n        [2.0592e+23, 8.1994e-10, 1.0538e-11]])"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.Tensor(2, 3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T11:10:34.801278Z",
     "end_time": "2023-04-29T11:10:34.820777Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[1., 0.],\n        [0., 1.]])"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.eye(2, 2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T11:12:52.776116Z",
     "end_time": "2023-04-29T11:12:52.798116Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([ 1.,  4.,  7., 10.])"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.linspace(1, 10, 4)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T11:13:22.155377Z",
     "end_time": "2023-04-29T11:13:22.178377Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[0.6477, 0.0012, 0.3144],\n        [0.7076, 0.3162, 0.6608]])"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.rand(2, 3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T11:14:13.131648Z",
     "end_time": "2023-04-29T11:14:13.183161Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "x = torch.randn(2, 3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T11:14:57.236746Z",
     "end_time": "2023-04-29T11:14:57.255868Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "torch.Size([2, 3])"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.size()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T11:15:09.947104Z",
     "end_time": "2023-04-29T11:15:09.956105Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "2"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.dim()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T11:15:26.569007Z",
     "end_time": "2023-04-29T11:15:26.592004Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 0.5340, -0.0787],\n        [ 0.1297,  2.0117],\n        [-0.4207, -2.8035]])"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.view(3, 2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T11:15:41.262689Z",
     "end_time": "2023-04-29T11:15:41.272689Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 0.5340, -0.0787,  0.1297],\n        [ 2.0117, -0.4207, -2.8035]])"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T11:15:50.853361Z",
     "end_time": "2023-04-29T11:15:50.874868Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [],
   "source": [
    "y = x.view(-1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T11:16:21.514385Z",
     "end_time": "2023-04-29T11:16:21.527390Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "torch.Size([6])"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.shape"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T11:16:29.865403Z",
     "end_time": "2023-04-29T11:16:29.903401Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([2.]) tensor([1.]) None\n",
      "None None\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\DYL\\AppData\\Local\\Temp\\ipykernel_9748\\3294004568.py:10: UserWarning: The .grad attribute of a Tensor that is not a leaf Tensor is being accessed. Its .grad attribute won't be populated during autograd.backward(). If you indeed want the .grad field to be populated for a non-leaf Tensor, use .retain_grad() on the non-leaf Tensor. If you access the non-leaf Tensor by mistake, make sure you access the leaf Tensor instead. See github.com/pytorch/pytorch/pull/30531 for more informations. (Triggered internally at C:\\cb\\pytorch_1000000000000\\work\\build\\aten\\src\\ATen/core/TensorBody.h:491.)\n",
      "  print(y.grad, z.grad)\n"
     ]
    }
   ],
   "source": [
    "x = torch.Tensor([2])\n",
    "w = torch.randn(1, requires_grad=True)\n",
    "b = torch.randn(1, requires_grad=True)\n",
    "\n",
    "y = torch.mul(w, x)\n",
    "z = torch.add(y, b)\n",
    "\n",
    "z.backward()\n",
    "print(w.grad, b.grad, x.grad)\n",
    "print(y.grad, z.grad)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T14:26:43.941198Z",
     "end_time": "2023-04-29T14:26:44.003726Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "1"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "torch.cuda.device_count()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2023-04-29T15:02:54.288893Z",
     "end_time": "2023-04-29T15:02:56.846961Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'tensorflow' has no attribute 'placeholder'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mAttributeError\u001B[0m                            Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[4], line 8\u001B[0m\n\u001B[0;32m      4\u001B[0m y \u001B[38;5;241m=\u001B[39m \u001B[38;5;241m3\u001B[39m \u001B[38;5;241m*\u001B[39m np\u001B[38;5;241m.\u001B[39mpower(x, \u001B[38;5;241m2\u001B[39m) \u001B[38;5;241m+\u001B[39m \u001B[38;5;241m2\u001B[39m \u001B[38;5;241m+\u001B[39m \u001B[38;5;241m0.2\u001B[39m \u001B[38;5;241m*\u001B[39m np\u001B[38;5;241m.\u001B[39mrandom\u001B[38;5;241m.\u001B[39mrand(x\u001B[38;5;241m.\u001B[39msize)\u001B[38;5;241m.\u001B[39mreshape(\u001B[38;5;241m100\u001B[39m, \u001B[38;5;241m1\u001B[39m)\n\u001B[0;32m      5\u001B[0m \u001B[38;5;66;03m# print(x, '\\n', y)\u001B[39;00m\n\u001B[0;32m      6\u001B[0m \n\u001B[0;32m      7\u001B[0m \u001B[38;5;66;03m# 初始化参数\u001B[39;00m\n\u001B[1;32m----> 8\u001B[0m x1 \u001B[38;5;241m=\u001B[39m \u001B[43mtf\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mplaceholder\u001B[49m(tf\u001B[38;5;241m.\u001B[39mfloat32, shape \u001B[38;5;241m=\u001B[39m (\u001B[38;5;28;01mNone\u001B[39;00m, \u001B[38;5;241m1\u001B[39m))\n",
      "\u001B[1;31mAttributeError\u001B[0m: module 'tensorflow' has no attribute 'placeholder'"
     ]
    }
   ],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
